package cn.hdustea.shopNavigation.ui.view

import android.content.Context
import android.content.Intent
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import cn.hdustea.shopNavigation.R
import cn.hdustea.shopNavigation.config.ConfigKey
import cn.hdustea.shopNavigation.entity.Market
import cn.hdustea.shopNavigation.entity.MarketSelect
import cn.hdustea.shopNavigation.ui.contract.MarketSelectContract
import cn.hdustea.shopNavigation.ui.presenter.MarketSelectPresenter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.hdustea.base.base.BaseActivity
import kotlinx.android.synthetic.main.activity_market_select.*

class MarketSelectActivity : BaseActivity<MarketSelectContract.Presenter>(), MarketSelectContract.View {

    private var mCurPosition = 0
    private val mGson = Gson()

    companion object {

        fun startActivity(context: Context) {
            context.startActivity(Intent(context, MarketSelectActivity::class.java))
        }

    }

    override fun initView() {
        val layoutManager = LinearLayoutManager(this)
        rv_city.layoutManager = layoutManager
        rv_city.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))

        val marketLayoutManager = LinearLayoutManager(this)
        rv_market.layoutManager = marketLayoutManager
    }

    override fun initData() {
        if (ConfigKey.getCityMarketsSelect() == "") {
            mPresenter?.getMarketList()
        } else {
            val marketList: MutableList<MarketSelect> =
                    mGson.fromJson(ConfigKey.getCityMarketsSelect(),
                            object : TypeToken<MutableList<MarketSelect>>() {}.type)
            setCityList(marketList)
        }
    }

    override fun getLayoutRes(): Int = R.layout.activity_market_select

    override fun getPresenter(): MarketSelectContract.Presenter = MarketSelectPresenter(this)

    override fun setCityList(marketList: MutableList<MarketSelect>) {

        val citySelectAdapter = CitySelectAdapter(R.layout.item_list_city_select)
        val marketSelectAdapter = MarketSelectAdapter(R.layout.item_list_market_select)
        rv_city.adapter = citySelectAdapter
        citySelectAdapter.setList(marketList)
        citySelectAdapter.setOnItemClickListener { adapter, view, position ->
            adapter.getViewByPosition(mCurPosition, R.id.tv_item_market_select)?.isSelected = false
            marketSelectAdapter.setList(marketList[position].markets)
            mCurPosition = position
            view.isSelected = true
        }

        rv_market.adapter = marketSelectAdapter
        marketSelectAdapter.setList(marketList[0].markets)
        marketSelectAdapter.setOnItemClickListener { adapter, _, position ->
            val market: Market = adapter.data[position] as Market
            ConfigKey.setGlobalLocation(market.name)
            finish()
        }
        val footerView = layoutInflater.inflate(R.layout.select_market_footer_view, rv_market, false)
        marketSelectAdapter.addFooterView(footerView)

        if (ConfigKey.getCityMarketsSelect() == "") {
            ConfigKey.setCityMarketsSelect(mGson.toJson(marketList))
        }

    }

    override fun onError() {

    }

    private inner class CitySelectAdapter(layoutRes: Int) : BaseQuickAdapter<MarketSelect, BaseViewHolder>(layoutRes) {

        override fun convert(holder: BaseViewHolder, item: MarketSelect) {
            holder.setText(R.id.tv_item_market_select, item.name)
            if (holder.layoutPosition == 0) {
                val view = holder.getView<AppCompatTextView>(R.id.tv_item_market_select)
                view.isSelected = true
            }
        }
    }

    private inner class MarketSelectAdapter(layoutRes: Int) :
            BaseQuickAdapter<Market, BaseViewHolder>(layoutRes) {

        override fun convert(holder: BaseViewHolder, item: Market) {
            holder.setText(R.id.tv_item_market_name, item.name)
            holder.setText(R.id.tv_item_market_distance, "${item.distance}km")
        }

    }

}